package com.atguigu03.threadsafe.runnablesafe.runnablesage;
/*
* 同步代码块-Runnable接口
* */
public class WindowsTest {
    public static void main(String[] args) {
        SaleTickets s=new SaleTickets();
        Thread t1=new Thread(s);
        Thread t2=new Thread(s);
        Thread t3=new Thread(s);

        t1.setName("线程1");
        t2.setName("线程2");
        t3.setName("线程3");

        t1.start();
        t2.start();
        t3.start();
    }
}

class SaleTickets implements Runnable{
    int ticket=100;
    Object o=new Object();
    @Override
    public void run() {
        while(true){
            synchronized (o){  //o是唯一的，因为，main中只有一个SaleTickets对象，其下属属性只有一个实例
                if(ticket>0){
                try {
                    Thread.sleep(10);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"售票，票号为："+ticket);
                ticket--;
            }else{
                break;
            }}

        }
    }
}
